package cn.iocoder.yudao.module.trade.verify.controller.app;

import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.verify.dal.dataobject.TradeVerifyRecordDO;
import cn.iocoder.yudao.module.trade.verify.service.TradeVerifyRecordService;
import cn.iocoder.yudao.module.trade.verify.vo.TradeVerifyRecordPageReqVO;
import cn.iocoder.yudao.module.trade.verify.vo.TradeVerifyRecordRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 核销记录")
@RestController
@RequestMapping("/trade/verify-record")
@Validated
public class AppTradeVerifyRecordController {

    @Resource
    private TradeVerifyRecordService verifyRecordService;

    @GetMapping("/get")
    @Operation(summary = "获得核销记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TradeVerifyRecordRespVO> getVerifyRecord(@RequestParam("id") Long id) {
        TradeVerifyRecordDO verifyRecord = verifyRecordService.getVerifyRecord(id);
        Assertions.notNull(verifyRecord, "核销记录不存在");
        Assertions.owner(verifyRecord.getUserId(), getLoginUserId());
        return success(BeanUtils.toBean(verifyRecord, TradeVerifyRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得核销记录分页")
    public CommonResult<PageResult<TradeVerifyRecordRespVO>> getVerifyRecordPage(@Valid TradeVerifyRecordPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());
        PageResult<TradeVerifyRecordDO> pageResult = verifyRecordService.getVerifyRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TradeVerifyRecordRespVO.class));
    }

}